package com.effectiv.gooruda.rest.api.controller.facebook;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.social.MissingAuthorizationException;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.FacebookProfile;
import org.springframework.social.facebook.api.FamilyMember;
import org.springframework.social.facebook.api.PagedList;
import org.springframework.social.facebook.api.PagingParameters;
import org.springframework.social.facebook.api.Reference;
import org.springframework.social.facebook.api.impl.FacebookTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api/facebook/friendoperations")
public class FriendOperationsController {

	@RequestMapping(value = "/addtofriendlist/{friendListId}/{friendId}", method = RequestMethod.POST)
	public void addToFriendList(
			@PathVariable("friendListId") String friendListId,
			@PathVariable("friendId") String friendId, Facebook facebook) {
		facebook.friendOperations().addToFriendList(friendListId, friendId);
	}

	@RequestMapping(value = "/createfriendlist/{name}", method = RequestMethod.POST)
	public @ResponseBody
	String createFriendList(@PathVariable("name") String name,
			FacebookTemplate facebook) {
		return facebook.friendOperations().createFriendList(name);
	}

	@RequestMapping(value = "/createfriendlist/{userId}/{name}", method = RequestMethod.POST)
	public @ResponseBody
	String createFriendList(@PathVariable("userId") String userId,
			@PathVariable("name") String name, FacebookTemplate facebook) {
		return facebook.friendOperations().createFriendList(userId, name);
	}

	@RequestMapping(value = "/deletefriendlist/{friendListId}", method = RequestMethod.POST)
	public void deleteFriendList(
			@PathVariable("friendListId") String friendListId,
			FacebookTemplate facebook) {
		facebook.friendOperations().deleteFriendList(friendListId);

	}

	@RequestMapping(value = "/getfamily", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<FamilyMember> getFamily(FacebookTemplate facebook) {
		return facebook.friendOperations().getFamily();
	}

	@RequestMapping(value = "/getfamily/{userId}", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<FamilyMember> getFamily(@PathVariable("userId") String userId,
			FacebookTemplate facebook) {
		return facebook.friendOperations().getFamily(userId);
	}

	@RequestMapping(value = "/getfriendids", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<String> getFriendIds(FacebookTemplate facebook) {
		return facebook.friendOperations().getFriendIds();
	}

	@RequestMapping(value = "/getfriendids/{userId}", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<String> getFriendIds(@PathVariable("userId") String userId,
			FacebookTemplate facebook) {
		return facebook.friendOperations().getFriendIds(userId);
	}

	@RequestMapping(value = "/getfriendlist/{friendListId}", method = RequestMethod.GET)
	public @ResponseBody
	Reference getFriendList(@PathVariable("friendListId") String friendListId,
			FacebookTemplate facebook) {
		return facebook.friendOperations().getFriendList(friendListId);
	}

	@RequestMapping(value = "/getfriendlistmembers/{friendListId}", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<Reference> getFriendListMembers(
			@PathVariable("friendListId") String friendListId,
			FacebookTemplate facebook) {
		return facebook.friendOperations().getFriendListMembers(friendListId);
	}

	@RequestMapping(value = "/getfriendlists", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<Reference> getFriendLists(FacebookTemplate facebook) {
		return facebook.friendOperations().getFriendLists();
	}

	@RequestMapping(value = "/getfriendlists/{userId}", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<Reference> getFriendLists(@PathVariable("userId") String userId,
			FacebookTemplate facebook) {
		return facebook.friendOperations().getFriendLists(userId);
	}

	@RequestMapping(value = "/getfriendprofiles", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<FacebookProfile> getFriendProfiles(
			PagingParameters pagedListParameters, FacebookTemplate facebook) {
		return facebook.friendOperations().getFriendProfiles(
				pagedListParameters);
	}

	@RequestMapping(value = "/getfriendprofiles/{offset}/{limit}", method = RequestMethod.GET)
	@Deprecated
	public @ResponseBody
	PagedList<FacebookProfile> getFriendProfiles(
			@PathVariable("offset") int offset,
			@PathVariable("limit") int limit, FacebookTemplate facebook) {
		return facebook.friendOperations().getFriendProfiles(offset, limit);
	}

	@RequestMapping(value = "/getfriendprofiles/{userId}", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<FacebookProfile> getFriendProfiles(
			@PathVariable("userId") String userId,
			PagingParameters pagedListParameters, FacebookTemplate facebook) {
		return facebook.friendOperations().getFriendProfiles(userId,
				pagedListParameters);
	}

	@RequestMapping(value = "/getfriendprofiles/{userId}/{offset}/{limit}", method = RequestMethod.GET)
	@Deprecated
	public @ResponseBody
	PagedList<FacebookProfile> getFriendProfiles(
			@PathVariable("userId") String userId,
			@PathVariable("offset") int offset,
			@PathVariable("limit") int limit, FacebookTemplate facebook) {
		return facebook.friendOperations().getFriendProfiles(userId, offset,
				limit);
	}

	@RequestMapping(value = "/getfriends", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<Reference> getFriends(FacebookTemplate facebook) {
		return facebook.friendOperations().getFriends();
	}

	@RequestMapping(value = "/getfriends/{userId}", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<Reference> getFriends(@PathVariable("userId") String userId,
			Facebook facebook) {
		return facebook.friendOperations().getFriends(userId);
	}

	@RequestMapping(value = "/getmutualfriends/{userId}", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<Reference> getMutualFriends(
			@PathVariable("userId") String userId, FacebookTemplate facebook) {
		return facebook.friendOperations().getMutualFriends(userId);
	}

	@RequestMapping(value = "/getsubscribedto", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<Reference> getSubscribedTo(FacebookTemplate facebook) {
		return facebook.friendOperations().getSubscribedTo();
	}

	@RequestMapping(value = "/getsubscribedto/{userId}", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<Reference> getSubscribedTo(@PathVariable("userId") String userId,
			Facebook facebook) {
		return facebook.friendOperations().getSubscribedTo(userId);
	}

	@RequestMapping(value = "/getsubscribers", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<Reference> getSubscribers(FacebookTemplate facebook) {
		return facebook.friendOperations().getSubscribers();
	}

	@RequestMapping(value = "/getsubscribers/{userId}", method = RequestMethod.GET)
	public @ResponseBody
	PagedList<Reference> getSubscribers(@PathVariable("userId") String userId,
			FacebookTemplate facebook) {
		return facebook.friendOperations().getSubscribers(userId);
	}

	@RequestMapping(value = "/removefromfriendlist/{friendListId}/{friendId}", method = RequestMethod.POST)
	public void removeFromFriendList(
			@PathVariable("friendListId") String friendListId,
			@PathVariable("friendId") String friendId, FacebookTemplate facebook) {
		facebook.friendOperations()
				.removeFromFriendList(friendListId, friendId);
	}

	@ExceptionHandler(MissingAuthorizationException.class)
	public @ResponseBody String handleMissingAuthorizationException(
			MissingAuthorizationException ex, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		response.sendError(HttpServletResponse.SC_CONFLICT);
		
		return ex.getLocalizedMessage();
	}
}
